package com.zeny.coding.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.SectionDto;
import com.zeny.coding.entity.Section;
import com.zeny.coding.entity.SectionExample;
import com.zeny.coding.mapper.SectionMapper;
import com.zeny.coding.service.CourseService;
import com.zeny.coding.service.SectionService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h2>小节服务</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@Service
public class SectionServiceImpl implements SectionService {

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private CourseService courseService;

    @Override
    public void list(PageDto<SectionDto> pageDto, String courseId, String chapterId) {
        //分页
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        //设置条件 课程id + 大章id
        SectionExample sectionExample = new SectionExample();
        SectionExample.Criteria criteria = sectionExample.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        criteria.andChapterIdEqualTo(chapterId);
        sectionExample.setOrderByClause("sort asc");
        //查询结果
        List<Section> sectionList = this.sectionMapper.selectByExample(sectionExample);
        PageInfo<Section> pageInfo = new PageInfo<>(sectionList);
        //设置总条数
        pageDto.setTotal(pageInfo.getTotal());
        //封装成DTO传输
        List<SectionDto> sectionDtoList = new ArrayList<>();
        for (Section section : sectionList) {
            SectionDto sectionDto = new SectionDto();
            BeanUtils.copyProperties(section, sectionDto);
            sectionDtoList.add(sectionDto);
        }
        //设置记录数
        pageDto.setList(sectionDtoList);
    }

    @Transactional
    @Override
    public void save(SectionDto sectionDto) {
        //DTO转~
        Section section = new Section();
        BeanUtils.copyProperties(sectionDto, section);
        //算法算法生成ID
        SnowFlake snowFlake = new SnowFlake(
                CourseConstants.dataCenterId,
                CourseConstants.machineId);
        section.setId(String.valueOf(snowFlake.nextId()));
        section.setCreatedAt(new Date());
        section.setUpdatedAt(section.getUpdatedAt());
        this.sectionMapper.insertSelective(section);
        //新增小节后更新课程总时长
        this.courseService.updateTime(section.getCourseId());
    }

    @Transactional
    @Override
    public void update(SectionDto sectionDto) {
        Section section = new Section();
        BeanUtils.copyProperties(sectionDto, section);
        section.setUpdatedAt(new Date());
        this.sectionMapper.updateByPrimaryKeySelective(section);
        //更新小节后更新课程总时长
        this.courseService.updateTime(section.getCourseId());
    }

    @Override
    public SectionDto findById(String id) {
        Section section = this.sectionMapper.selectByPrimaryKey(id);
        SectionDto sectionDto = new SectionDto();
        BeanUtils.copyProperties(section, sectionDto);
        return sectionDto;
    }

    @Override
    public void deleteById(String id) {
        this.sectionMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void deleteByCourseId(String courseId) {
        SectionExample sectionExample = new SectionExample();
        sectionExample.createCriteria().andCourseIdEqualTo(courseId);
        this.sectionMapper.deleteByExample(sectionExample);
    }

    @Transactional
    @Override
    public void deleteByChapterId(String chapterId) {
        SectionExample sectionExample = new SectionExample();
        sectionExample.createCriteria().andChapterIdEqualTo(chapterId);
        this.sectionMapper.deleteByExample(sectionExample);
    }

    @Override
    public List<Section> findByChapterId(String chapterId) {
        SectionExample example = new SectionExample();
        example.createCriteria().andChapterIdEqualTo(chapterId);
        example.setOrderByClause("sort");
        return sectionMapper.selectByExample(example);
    }
}
